<?php

require_once __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

try {
    echo "📊 完整菜单列表：\n";
    $menus = \DB::table('menus')->orderBy('sort_order')->get();

    foreach ($menus as $menu) {
        $parentInfo = $menu->parent_id ? " (父菜单ID: {$menu->parent_id})" : " (根菜单)";
        echo "  - ID: {$menu->id}, 名称: {$menu->name}, 路径: {$menu->path}{$parentInfo}\n";
    }

    echo "\n📊 角色权限检查：\n";
    $superAdminRole = \DB::table('roles')->where('code', 'super_admin')->first();
    if ($superAdminRole) {
        $permissions = json_decode($superAdminRole->permissions, true);
        echo "超级管理员权限：" . (in_array('*', $permissions) ? "所有权限（*）" : implode(', ', $permissions)) . "\n";
    }

    echo "\n📊 用户角色关联：\n";
    $adminUser = \DB::table('users')->where('username', 'admin')->first();
    if ($adminUser) {
        $userRoles = \DB::table('user_roles')
            ->join('roles', 'user_roles.role_id', '=', 'roles.id')
            ->where('user_roles.user_id', $adminUser->id)
            ->where('user_roles.status', 'active')
            ->select('roles.name', 'roles.code')
            ->get();

        foreach ($userRoles as $role) {
            echo "  - 用户 {$adminUser->username} 拥有角色：{$role->name} ({$role->code})\n";
        }
    }

} catch (Exception $e) {
    echo "❌ 错误：" . $e->getMessage() . "\n";
}
